package com.example.smartall.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.OrderDetailsBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;

public class OrderDetailsActivity extends BaseActivity {

    private String orderName;
    private int orderId;

    private ImageView imBack;
    private TextView tvTitle;
    private ImageView imBim;
    private ImageView imLim;
    private TextView tvName;
    private TextView tvIntroduction;
    private TextView tvAddress;
    private TextView tvScore;
    private RatingBar rbBar;
    private TextView deliveryTime;
    private TextView distance;
    private TextView saleQuantity;
    private TextView saleNum3hour;
    private TextView themeId;
    private TextView avgCost;
    private ListView lvFood;

    private OrderDetailsBean.DataBean data;

    private MHandler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);

        orderName = getIntent().getStringExtra("orderName");
        orderId = getIntent().getIntExtra("orderId",-1);

        handler = new MHandler();

        init();
        getOrderDetails();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        imBim = (ImageView) findViewById(R.id.im_bim);
        imLim = (ImageView) findViewById(R.id.im_lim);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvIntroduction = (TextView) findViewById(R.id.tv_introduction);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvScore = (TextView) findViewById(R.id.tv_score);
        rbBar = (RatingBar) findViewById(R.id.rb_bar);
        deliveryTime = (TextView) findViewById(R.id.deliveryTime);
        distance = (TextView) findViewById(R.id.distance);
        saleQuantity = (TextView) findViewById(R.id.saleQuantity);
        saleNum3hour = (TextView) findViewById(R.id.saleNum3hour);
        themeId = (TextView) findViewById(R.id.themeId);
        avgCost = (TextView) findViewById(R.id.avgCost);
        lvFood = (ListView) findViewById(R.id.lv_food);

        tvTitle.setText("店家详情");
        tvName.setText(orderName);
    }

    private void initData(){
        Glide.with(OrderDetailsActivity.this)
                .load(ApiConfig.BASE_URL + data.getImgUrl())
                .into(imBim);
        Glide.with(OrderDetailsActivity.this)
                .load(ApiConfig.BASE_URL + data.getImgUrl())
                .into(imLim);
        tvIntroduction.setText(data.getIntroduction());
        tvAddress.setText(data.getAddress());
        tvScore.append(data.getScore()+"");
        rbBar.setRating((float) data.getScore());
        deliveryTime.append(data.getDeliveryTime()+"分钟");
        distance.append(data.getDistance()+"米");
        saleQuantity.append(data.getSaleQuantity()+"份");
        saleNum3hour.append(data.getSaleNum3hour()+"份");
        themeId.append(data.getThemeId()+"");
        avgCost.append(data.getAvgCost()+"元");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderDetailsActivity.this.finish();
            }
        });
    }

    private void getOrderDetails(){
        Api.config(ApiConfig.ORDER_DETAILS,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                OrderDetailsBean bean = new Gson().fromJson(res,OrderDetailsBean.class);
                if (bean.getCode() == 200){
                    data = bean.getData();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },orderId+"");
    }

    class MHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x01:
                    initData();
                    break;
                default:
                    break;
            }
        }
    }
}